#! /usr/bin/python
# _*_ coding:UTF-8 _*_
import matplotlib.pyplot as plt
import numpy as np
import random
from multiprocessing import Process


def visit_data(data):
    plt.figure(1)
    plt.plot(data[:, 0], data[:, 1], 'rx', label='sss')
    plt.xlabel('house size')
    plt.ylabel("price")
    plt.title('line_regression')
    plt.legend()
    plt.draw()
    plt.show()
    return


def plot_line(X, theta):
    line_x = np.arange(0, 25, 1)
    line_y = theta[0, 0] + theta[1, 0]*line_x
    plt.figure(1)
    plt.plot(data[:, 0], data[:, 1], 'bx', label='sss')
    plt.plot(line_x, line_y, 'r', label='sss')
    plt.xlabel('house size')
    plt.ylabel("price")
    plt.title('line_regression')
    plt.legend()
    plt.show()


def cost_function(X, theta, y):
    m = X.shape[0]
    X = np.c_[np.ones((m, 1)), X]
    cost = np.sum(pow(X.dot(theta) - y, 2))
    return cost


def grade(X, theta, y, alpha):
    (m, n) = X.shape
    (theta_m, theta_n) = theta.shape
    X = np.c_[np.ones((m, 1)), X]
    theta = theta - (alpha/m) * np.sum((X.dot(theta) - y)*X, axis=0).reshape(theta_m,theta_n)
    return theta


if __name__ == "__main__" :
    data = np.loadtxt('ex1data1.txt', delimiter=',')
    (m,n) = data.shape
    X = data[:, 0].reshape(m, 1)
    y = data[:, 1].reshape(m, 1)
    print '线性回归练习'
    print '可视化数据集'
    # p = Process(target=visit_data, args=[data])
    # p.start()
    # raw_input('按回车键继续.....')
    theta = np.array([[2],[3]])
    cost_function(X, theta, y)
    # 随机生成theta
    theta[0, 0] = random.random()
    theta[1, 0] = random.random() * 10

    # 执行梯度下降
    i = 0
    alpha = 0.02  #学习率
    while(i<1000):
        cost = cost_function(X, theta, y)  # 计算代价
        theta = grade(X, theta, y, alpha)
        print "第%d迭代: cost=%f, 梯度[%f, %f]"%(i+1, cost, theta[0, 0], theta[1, 0])
        i+=1
    plot_line(data, theta)



